package com.itheima.web;

import com.alibaba.fastjson.JSON;

import com.hmall.ItemClient;

import com.hmall.common.dto.Item;
import com.hmall.common.dto.ItemDoc;
import com.hmall.common.dto.PageDTO;
import com.itheima.pojo.RequestParams;
import com.itheima.service.SearchabcService;
import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.search.suggest.Suggest;
import org.elasticsearch.search.suggest.SuggestBuilder;
import org.elasticsearch.search.suggest.SuggestBuilders;
import org.elasticsearch.search.suggest.completion.CompletionSuggestion;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/search")
public class SearchController {

    @Autowired
    private ItemClient itemClient;

    @Autowired
    private RestHighLevelClient client;

    @Autowired
    private SearchabcService searchabcService;


    @GetMapping("/sxl")
    public String test() throws Exception{
        itemClient.findPage(1l, 1l);
        //        long start = System.currentTimeMillis();
//        PageDTO<Item> page = itemClient.findPage(1l, 1l);
//        for (int i = 1; i <= (page.getTotal()/2000)+1; i++) {
//            System.out.println(i);
//            PageDTO<Item> result = itemClient.findPage((long)i, 2000l);
//            BulkRequest bulkRequest = new BulkRequest();
//            for (Item item : result.getList()) {
//                ItemDoc itemDoc = new ItemDoc(item);
//                String jsonString = JSON.toJSONString(itemDoc);
//                IndexRequest request = new IndexRequest("hmmall");
//                request.id(item.getId()+"");
//                request.source(jsonString, XContentType.JSON);
//                bulkRequest.add(request);
//            }
//            client.bulk(bulkRequest, RequestOptions.DEFAULT);
//        }
//        long end = System.currentTimeMillis();
//        long start1 = end - start;
        return "总共耗时";
    }

    @GetMapping("/suggestion")
    public List<String> AutoSuggestion1(@RequestParam("key")String key){
        System.out.println("key = " + key);
        return searchabcService.AutoSuggestion(key);
    }

    @PostMapping("/filters")
    public Map<String, List<String>> getFilters(@RequestBody RequestParams params){
        return searchabcService.getFilters(params);
    }

    @PostMapping("/list")
    public PageDTO<ItemDoc> basicSearch(@RequestBody RequestParams requestParam){
        return searchabcService.basicSearch(requestParam);
    }


}
